package com.example.demo.controller;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.demo.common.Result;
import com.example.demo.entity.Goods;
import com.example.demo.mapper.GoodsMapper;
import com.example.demo.mapper.UserMapper;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;

/**
 * @Author: likew
 * @Date: 2022/4/10
 */

@RestController
@RequestMapping("/busgood")
public class BusGoodController extends BaseController {
    @Resource
    GoodsMapper goodsMapper;
    
    @Resource
    UserMapper userMapper;
    
    
    @PostMapping
    public Result<?> save(@RequestBody Goods Goods) {
        goodsMapper.insert(Goods);
        return Result.success();
    }
    
    @PutMapping
    public Result<?> update(@RequestBody Goods Goods) {
        goodsMapper.updateById(Goods);
        return Result.success();
    }
    
    @DeleteMapping("/{id}")
    public Result<?> delete(@PathVariable Integer id) {
        goodsMapper.deleteById(id);
        return Result.success();
    }
    
    @PostMapping("/deleteBatch")
    public Result<?> deleteBatch(@RequestBody List<Integer> ids) {
        goodsMapper.deleteBatchIds(ids);
        return Result.success();
    }
    
    @PostMapping("/{id}")
    public Result<?> getById(@PathVariable Integer id) {
        return Result.success(goodsMapper.findByBusId(id));
    }
    
    /**
     * 注意：这个方法使用的是Mybatis sql的方式做的多表联合查询
     * @param
     * @return
     */
    @GetMapping("/{busId}")
    public Result<?> getByUserId(@PathVariable Integer busId) {
        return Result.success(goodsMapper.findByBusId(busId));
    }
    
    @GetMapping
    public Result<?> findPage(@RequestParam(defaultValue = "1") Integer pageNum,
            @RequestParam(defaultValue = "10") Integer pageSize,
            @RequestParam(defaultValue = "") String search,
            @RequestParam(defaultValue = "1") Integer id) {
        LambdaQueryWrapper<Goods> wrapper = Wrappers.<Goods>lambdaQuery();
        if (StrUtil.isNotBlank(search)) {
            wrapper.like(Goods::getName, search);
        }
        //对查询到的id进行匹配，确认是否是该账号下所查询的
        wrapper.eq(Goods::getBusId,id);
        Page<Goods> BookPage = goodsMapper.selectPage(new Page<>(pageNum, pageSize), wrapper);
        return Result.success(BookPage);
    }
}
